//
//  RecommedViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/4.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "RecommedViewController.h"

@interface RecommedViewController ()
{
    RecommedView *_recommedView;
    NSMutableArray *_dataArr;
    
    // 分组数组
    NSMutableArray *_newSongsArr;
    NSMutableArray *_hotSongsArr;
    NSMutableArray *_titleSongsArr;
    NSMutableArray *_peopleSeeArr;
    NSMutableArray *_asiaArr;
    NSMutableArray *_personArr;
    NSMutableArray *_otherArr;
    
    MBProgressHUD *HUD;
    NSMutableArray *modelArr;
    DanLiPlayer *BarPlayer;

    
}

@property (nonatomic, retain) NSMutableArray *newSongsArr;



@end

@implementation RecommedViewController


- (NSMutableArray *)newSongsArr
{
    if (!_newSongsArr) {
        self.newSongsArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _newSongsArr;
}

- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [HUD removeFromSuperview];
    HUD = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    

    
    
    
    _dataArr = [[NSMutableArray alloc] init];    
    _newSongsArr = [[NSMutableArray alloc] init];
    _hotSongsArr = [[NSMutableArray alloc] init];
    _titleSongsArr = [[NSMutableArray alloc] init];
    _peopleSeeArr = [[NSMutableArray alloc] init];
    _asiaArr = [[NSMutableArray alloc] init];
    _personArr = [[NSMutableArray alloc] init];
    _otherArr = [[NSMutableArray alloc] init];

    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _recommedView = [[RecommedView alloc] initWithFrame:
                     CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _recommedView.recommedTableView.dataSource = self;
    _recommedView.recommedTableView.delegate = self;
    _recommedView.delegate = self;
    _recommedView.recommedTableView.backgroundColor = [UIColor clearColor];
    _recommedView.recommedTableView.separatorColor = [UIColor clearColor];
    
    [self.view addSubview:_recommedView];
    [_recommedView release];
    
    
    [self getHTTPresponse];
    
    // 小菊花
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"正在加载...";
    [self.view addSubview:HUD];
    [HUD setMode:MBProgressHUDModeIndeterminate];
    
    // 自动下拉刷新
    [_recommedView.recommedTableView headerBeginRefreshing];
    
    //下拉刷新
    [_recommedView.recommedTableView addHeaderWithCallback:^{

        [_dataArr removeAllObjects];
        [_recommedView.recommedTableView reloadData];
        [self getHTTPresponse];
        // 2.2秒后刷新表格UI
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [_recommedView.recommedTableView headerEndRefreshing];
        });
    }];
    
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    
    
    // 再次请求数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHttpDataFortheAPP) name:@"Internet" object:nil];
    
    
}

// 再次请求数据
- (void)getHttpDataFortheAPP
{
    NSLog(@"再次加载页面");
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"正在加载...";
    [self.view addSubview:HUD];
    HUD.color = COLOR(64, 165, 208, 1.0);
    [HUD setMode:MBProgressHUDModeIndeterminate];
    
    [_recommedView.recommedTableView headerBeginRefreshing];
    
    HUD.hidden = YES;
    
    
//    [mb showAnimated:YES whileExecutingBlock:^{
//        sleep(1);
//    } completionBlock:^{
//        [mb removeFromSuperview];
//    }];
    
}


- (void)getHTTPresponse
{
    
    
    [AFN GETwithAFNByURL:RecommedUrl completion:^(id responseObject) {
        
        // 如果请求数据为空, 提示用户
        if (responseObject == nil) {
            
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"网络状态不佳" message:@"是否刷新" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            [alter show];
            [alter release];
            HUD.hidden = YES;
            
        } else {
            
            NSDictionary *dataDic = [responseObject objectForKey:@"data"];
            NSMutableArray *songListArr = [dataDic objectForKey:@"songlists"];
            // 轮播图获得数据
            [_recommedView getTitleImage:songListArr[0]];
            
            for (NSDictionary *dic in songListArr) {
                RecommedModel *model = [[RecommedModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_dataArr addObject:model];
                [model release];
            }
            HUD.hidden = YES;
            // 刷新表格
            [_recommedView.recommedTableView reloadData];
        }
    }];
}


// 判断网络状态, 继续刷新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    switch (buttonIndex) {
        case 0:
            
            NSLog(@"1111");
            
            break;
        case 1:
            
            [self getHttpDataFortheAPP];
            
        default:
            break;
    }
    
}



#pragma mark -- ScrollView delegate
- (void)gotoNextViewController:(RecommedModel *)ReCommedModel
{
    
    NormalViewController *normalVc = [[[NormalViewController alloc] init] autorelease];
    
    normalVc.recommedModel = ReCommedModel;
    
    [self.navigationController pushViewController:normalVc animated:YES];
    
    
}

#pragma mark -- ReTableViewCell2 delegate
- (void)gotoHotListViewControllerWithTag:(RecommedModel *)recommedModel
{
    NormalViewController *normalVc = [[[NormalViewController alloc] init] autorelease];
    
    normalVc.recommedModel = recommedModel;
    
    [self.navigationController pushViewController:normalVc animated:YES];
}





- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return @"最新音乐";
    } else if (section == 1) {
       
        return @"热门歌单";
        
    } else if (section == 2) {
        
        return @"其他歌单";
    } else if (section == 3) {
        
        return @"大家在看";
    } else if (section == 4) {      
        
        return @"亚洲新歌榜";
        
    } else if (section == 5) {
        
        return @"个性推荐";
    } else {
        
        return @"网友热推";
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
    
}

// 判断每个section的 cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
    if (section == 0) {
        return 2;
    } else if (section == 6) {
        return 2;
    } else {
        return 1;
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        static NSString *cellId = @"cell";
        ReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            cell = [[ReTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        // 标签是 最新音乐 的model 取出来
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"最新音乐"]) {
                [_newSongsArr addObject:model];
            }
            if (_newSongsArr.count > indexPath.row) {
                RecommedModel *model = _newSongsArr[indexPath.row];
                NSDictionary *dic = [model.data objectAtIndex:indexPath.row];
                [cell getDataDic:dic];
            }
        }        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    } else if (indexPath.section == 1) {
        static NSString *cellId = @"HotCell";
        ReTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            cell = [[ReTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"热门歌单"]) {
                [_hotSongsArr addObject:model];
            }
            if (_hotSongsArr.count > indexPath.row) {
                // 取出数组数据
                RecommedModel *model = _hotSongsArr[indexPath.row];
                NSArray *arr = model.data;
                [cell getHotMusicDataArr:arr];
            }
        }
        // 签订协议
        cell.delegate = self;

        return cell;
        
    } else if (indexPath.section == 2) {
        static NSString *cellId = @"HotCell";
        ReTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            
            cell = [[ReTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        for (RecommedModel *model in _dataArr) {
            
            if ([model.desc isEqualToString:@"主题类歌单"]) {
                [_titleSongsArr addObject:model];
            }
            if (_titleSongsArr.count > indexPath.row) {
                
                RecommedModel *model = _titleSongsArr[indexPath.row];
                NSArray *arr = model.data;
                [cell getHotMusicDataArr:arr];
            }
        }
        
        cell.delegate = self;
        return cell;
        
    } else if (indexPath.section == 3) {
        // 大家在看的cell
        static NSString *cellId = @"PeopleCell";
        
        Re_PeopleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            
            cell = [[Re_PeopleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"大家在看"]) {
                [_peopleSeeArr addObject:model];
            }
            if (_peopleSeeArr.count > indexPath.row) {
                
                RecommedModel *model = _peopleSeeArr[indexPath.row];
                NSDictionary *dic = [model.data objectAtIndex:0];
                [cell getDataForImage:dic];
            }
        }
        

        return cell;
        
    } else if (indexPath.section == 4) {
        
        static NSString *cellId = @"cell";
        ReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            
            cell = [[ReTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"亚洲新歌榜"]) {
                [_asiaArr addObject:model];
            }
            if (_asiaArr.count > indexPath.row) {
                
                RecommedModel *model = _asiaArr[indexPath.row];
                NSDictionary *dic = model.data[0];
                [cell getDataDic:dic];
            }
        }
        // 小箭头

        return cell;
        
        
    } else if (indexPath.section == 5) {
        
        static NSString *cellId = @"cell";
        ReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            
            cell = [[ReTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"个性化推荐"]) {
                [_personArr addObject:model];
            }
            if (_personArr.count > indexPath.row) {
                RecommedModel *model = [_personArr objectAtIndex:indexPath.row];
                NSDictionary *dic = [model.data objectAtIndex:0];
                [cell getDataDic:dic];
            }
        }

        return cell;
        
    } else if (indexPath.section == 6) {
        static NSString *cellId = @"cell";
        ReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == NO) {
            
            cell = [[ReTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        for (RecommedModel *model in _dataArr) {
            if ([model.name isEqualToString:@"网友热推"]) {
                [_otherArr addObject:model];
            }
            if (_otherArr.count > indexPath.row) {
                RecommedModel *model = [_otherArr objectAtIndex:indexPath.row];
                NSDictionary *dic = [model.data objectAtIndex:indexPath.row];
                [cell getDataDic:dic];
            }
        }

        return cell;
        
    }
    
    return nil;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section > 0 && indexPath.section < 3) {
        
        return 170*ADJUSTWIDTH; // 160
    } else if (indexPath.section == 3) {
        
        return 190*ADJUSTWIDTH;
    }
    else {
        
        return 110*ADJUSTWIDTH; // 130
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        // 最新音乐
        ReDetailViewController *redetailVc = [[ReDetailViewController alloc] init];
        if (_newSongsArr.count > indexPath.row) {
            RecommedModel *recommed = [_newSongsArr objectAtIndex:indexPath.row];
            
            redetailVc.recommedModel = recommed;
            
            [self.navigationController pushViewController:redetailVc animated:YES];
        }
        

        
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        
        // 最新专辑
        ListNewMusicController *listVc = [[[ListNewMusicController alloc] init] autorelease];
        
        [self.navigationController pushViewController:listVc animated:YES];
        

        
    }  else if (indexPath.section == 3) {
        
        // MV
        MvViewController *mvVc = [[MvViewController alloc] init];
        
        [self.navigationController pushViewController:mvVc animated:YES];

        
    } else if (indexPath.section == 4) {
        
        
        // 正常的歌单页面
        NormalViewController *normalVc = [[NormalViewController alloc] init];
        RecommedModel *model = [[RecommedModel alloc] init];
        
        if (_asiaArr.count > indexPath.row) {
            RecommedModel *dicModel = [_asiaArr objectAtIndex:0];
            NSArray *ArrModel = dicModel.data;
            [model setValuesForKeysWithDictionary:[ArrModel objectAtIndex:0]];
            normalVc.recommedModel = model;
            [self.navigationController pushViewController:normalVc animated:YES];

        }
        
    } else if (indexPath.section == 5) {
        
        // 猜你喜欢
        GuessViewController *guessVc = [[[GuessViewController alloc] init] autorelease];        
        [self.navigationController pushViewController:guessVc animated:YES];
        
    } else if (indexPath.section == 6 && indexPath.row == 0) {
        
        
        // 网友热推
        NormalViewController *normalVc = [[NormalViewController alloc] init];
        RecommedModel *model = [[RecommedModel alloc] init];
        
        if (_otherArr.count > indexPath.row) {
            RecommedModel *dicModel = [_otherArr objectAtIndex:0];
            NSArray *ArrModel = dicModel.data;
            [model setValuesForKeysWithDictionary:[ArrModel objectAtIndex:0]];
            normalVc.recommedModel = model;
            [self.navigationController pushViewController:normalVc animated:YES];
        }
        
    } else if (indexPath.section == 6 && indexPath.row == 1) {
        // 大家在听
        PeopleListenViewController *peopVc = [[[PeopleListenViewController alloc] init] autorelease];
        [self.navigationController pushViewController:peopVc animated:YES];
    } else {
        
        NSLog(@"");
    }
    
     
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return  nil;
    }
    

    

    
    UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)] autorelease];
    header.backgroundColor = [UIColor blackColor];
    header.alpha = 0.7;
  
    UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(4, 1, 4, 25)];
    sectionHeader.backgroundColor = COLOR(181, 50, 124, 1.0);
    sectionHeader.alpha = 0.6;
    [header addSubview:sectionHeader];
    
    
    UILabel * label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(9, 0, 100, 30);
    //    label.backgroundColor = [UIColor clearColor];
    //    label.backgroundColor = [UIColor redColor];
    label.font=[UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;
    label.textColor = [UIColor whiteColor];
      [header addSubview:label];
    
   
    
    return header;
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
